package org.sean.generator.manager;

import org.sean.framework.bean.PageData;
import org.sean.framework.validator.Assert;
import org.sean.framework.validator.ValidatorUtils;
import org.sean.framework.validator.group.ADD;
import org.sean.framework.validator.group.ALL;
import org.sean.framework.validator.group.UPDATE;
import org.sean.generator.dto.DataSource;
import org.sean.generator.dto.DataSourceQuery;
import org.sean.generator.service.DataSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Coder Generator
 */
@Service
public class DataSourceManager {
    @Autowired
    private DataSourceService dataSourceService;


    /**
     * 分页查询
     *
     * @param query 条件
     * @return 分页数据
     */
    public PageData<DataSource> page(DataSourceQuery query) {
        return dataSourceService.pageDTO(query);
    }

    /**
     * 全部数据
     */
    public List<DataSource> list() {
        DataSourceQuery query = new DataSourceQuery();
        query.setSize(100);
        return dataSourceService.pageDTO(query).getList();
    }

    /**
     * 根据ID获取
     *
     * @param id id
     * @return 实体信息
     */
    public DataSource get(Long id) {
        return dataSourceService.getDTO(id);
    }

    /**
     * 保存
     *
     * @param data 实体信息
     */
    public Long save(DataSource data) {
        //效验数据
        ValidatorUtils.validate(data, ADD.class, ALL.class);
        return dataSourceService.saveDTO(data).getId();
    }

    /**
     * 修改
     *
     * @param data 实体信息
     */
    public void update(DataSource data) {
        //效验数据
        ValidatorUtils.validate(data, UPDATE.class, ALL.class);
        dataSourceService.updateDTO(data);
    }

    /**
     * 根据ID删除
     *
     * @param id id
     */
    public void delete(Long id) {
        //效验数据
        Assert.isValidId(id, "id不合法");
        dataSourceService.removeById(id);
    }

    /**
     * 根据ID删除
     *
     * @param ids id
     */
    public void delete(Long[] ids) {
        //效验数据
        Assert.isEmpty(ids, "ids不合法");
        dataSourceService.deleteByIds(ids);
    }

}